/**
 * 
 */
package com.byzk.p2p.admin.user.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.fuiou.config.Config;
import com.byzk.p2p.admin.fuiou.dto.CallBackDto;
import com.byzk.p2p.admin.fuiou.util.SecurityUtils;
import com.byzk.p2p.admin.fuiou.util.WebUtils;
import com.byzk.p2p.admin.fuiou.util.XmlUtils;
import com.byzk.p2p.admin.system.dto.Callback;
import com.byzk.p2p.admin.user.dto.UserAccount;
import com.byzk.p2p.admin.user.service.NoticeCallBackService;

/*** <p>Title:NoticeCallBackController </p>
* <p>Description:通知回调 </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年10月17日上午8:35:04 */

@RequestMapping(value="/callback")
@Controller
public class NoticeCallBackController {
    
	@Autowired
	private  NoticeCallBackService noticeCallbackService;
	
	private  static Logger logger = Logger.getLogger(NoticeCallBackController.class);
	
	/**
	 * 
	* <p>Description:充值通知 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	* @date:2016年10月17日上午8:40:24
	 */

	@RequestMapping(value = "/recharge",method=RequestMethod.POST)
	public void rechargeNotice(HttpServletRequest request, HttpServletResponse response) {

		logger.info("=======调用充值通知接口开始=======,时间[" + DateUtils.getNowTimeString() + "]");
		String mchnt_cd = request.getParameter("mchnt_cd");
		String mchnt_txn_ssn = request.getParameter("mchnt_txn_ssn");
		String mchnt_txn_dt = request.getParameter("mchnt_txn_dt");
		String mobile_no = request.getParameter("mobile_no");
		String amt = request.getParameter("amt");
		String remark = request.getParameter("remark");
		String signature = request.getParameter("signature");
		logger.info("===========参数列表===============："+request.getParameterMap());
		CallBackDto dto = new CallBackDto();
		Map<String, String> paramMap = new HashMap<String, String>();
		try{
			paramMap.put("mchnt_cd", mchnt_cd);
			paramMap.put("mchnt_txn_ssn", mchnt_txn_ssn);
			paramMap.put("mchnt_txn_dt", mchnt_txn_dt);
			paramMap.put("mobile_no", mobile_no);
			paramMap.put("amt", amt);
			paramMap.put("remark", remark);
			paramMap.put("signature", signature);
			noticeCallbackService.recharge(paramMap);
			//响应请求
			noticeCallbackService.responseSucc(response,paramMap);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
	}
	
	/**
	 * 
	* <p>Description:提现通知 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	* @date:2016年10月17日上午8:41:27
	 */
	@RequestMapping(value="/withdraw",method=RequestMethod.POST)
	public  void  withdrawNotice(HttpServletRequest request, HttpServletResponse response){
		
		logger.info("=======调用提现通知接口开始=======,时间[" + DateUtils.getNowTimeString() + "]");
		String mchnt_cd = request.getParameter("mchnt_cd");
		String mchnt_txn_ssn = request.getParameter("mchnt_txn_ssn");
		String mchnt_txn_dt = request.getParameter("mchnt_txn_dt");
		String mobile_no = request.getParameter("mobile_no");//手机号
		String amt = request.getParameter("amt");//金额
		String remark = request.getParameter("remark");
		String signature = request.getParameter("signature");
		logger.info("===========参数列表===============："+request.getParameterMap());
		CallBackDto dto = new CallBackDto();
		Map<String, String> paramMap = new HashMap<String, String>();
		try{
			paramMap.put("mchnt_cd", mchnt_cd);
			paramMap.put("mchnt_txn_ssn", mchnt_txn_ssn);
			paramMap.put("mchnt_txn_dt", mchnt_txn_dt);
			paramMap.put("mobile_no", mobile_no);
			paramMap.put("amt", amt);
			paramMap.put("remark", remark);
			paramMap.put("signature", signature);
			noticeCallbackService.withDraw(paramMap);
			//响应请求
			noticeCallbackService.responseSucc(response,paramMap);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
		
	}
	/**
	 * 
	* <p>Description:变更银行卡通知接口 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	* @date:2016年10月17日上午8:42:33
	 */
	@RequestMapping(value="/changeBank",method=RequestMethod.POST)
	public  void  changeBankNotice(HttpServletRequest request, HttpServletResponse response){
		
		logger.info("=======调用用户修改信息通知接口开始=======,时间[" + DateUtils.getNowTimeString() + "]");
		String mchnt_cd = request.getParameter("mchnt_cd");//商户代码
		String mchnt_txn_ssn = request.getParameter("mchnt_txn_ssn");//流水号
		String user_id_from = request.getParameter("user_id_from");//用户商户系统标志
		String mobile_no = request.getParameter("mobile_no");//手机号
		String cust_nm = request.getParameter("cust_nm");//客户姓名
		String certif_id = request.getParameter("certif_id");//身份证号码
		String email = request.getParameter("email");//邮箱
		String city_id = request.getParameter("city_id");//开户行地区代码
		String parent_bank_id = request.getParameter("parent_bank_id");//开户行行别
		String bank_nm = request.getParameter("bank_nm");//开户行支行名称
		String capAcntNo = request.getParameter("capAcntNo");//账号
		String resp_code = request.getParameter("resp_code");//返回码
		String signature = request.getParameter("signature");//签名数据
		logger.info("===========参数列表===============："+request.getParameterMap());
		CallBackDto dto = new CallBackDto();
		Map<String, String> paramMap = new HashMap<String, String>();
		try{
			paramMap.put("mchnt_cd", mchnt_cd);
			paramMap.put("mchnt_txn_ssn", mchnt_txn_ssn);
			paramMap.put("user_id_from", user_id_from);
			paramMap.put("mobile_no", mobile_no);
			paramMap.put("cust_nm", cust_nm);
			paramMap.put("certif_id", certif_id);
			paramMap.put("email", email);
			paramMap.put("city_id", city_id);
			paramMap.put("parent_bank_id", parent_bank_id);
			paramMap.put("bank_nm", bank_nm);
			paramMap.put("capAcntNo", capAcntNo);
			paramMap.put("resp_code", resp_code);
			paramMap.put("signature", signature);
			
			noticeCallbackService.modifyBank(paramMap);
			//响应请求
			noticeCallbackService.responseSucc(response,paramMap);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
	}
	
	
	public static void main(String [] args){
		UserAccount account = new UserAccount();
		CallBackDto dto = new CallBackDto();
	 	dto.setMchnt_cd(Config.mchnt_cd);
//		dto.setMchnt_txn_ssn(DateUtils.getFullFormat() + (new Random().nextInt(8999) + 1000));
	 	dto.setMchnt_txn_ssn("201611021144053257");
		dto.setMobile_no("15019460202");
		dto.setMchnt_txn_dt(DateUtils.getNowTimeString());
		dto.setAmt(100);
		String inputStr = dto.regSignVal();
		dto.setSignature(SecurityUtils.sign(inputStr));
		String backStr;
		try{
			backStr = WebUtils.sendHttp(Config.server_url+"/callback/withdraw", dto);
			logger.info("接口调用信息"+backStr);
			Callback callback = new Callback("--------查询客户金账户可用余额------", backStr, new Date());
			String resp_code = XmlUtils.getXmlNodeValue(backStr, "resp_code");
		}catch(Exception e){
			logger.info("=====请求接口失败======"+e.getMessage());
		}
	}
	

}
